<template>
  <div>
    <el-button :icon="Plus" type="primary" :disabled="!categoryStore.category3Id" @click="emits('changeScene',2)">添加SPU</el-button>
    <el-table border :data="spuList" style="margin:10px 0">
      <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column label="SPU名称" prop="spuName" width="280px"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button size="small" :icon="Plus" type="primary" @click="toAddskuInfo(row)"/>
          <el-button size="small" :icon="Edit" type="primary" @click="toEditSpuInfo(row)" />
          <el-button size="small" :icon="InfoFilled" type="info" @click="toCheckSkuList(row)" />
          <el-button size="small" :icon="Delete" type="danger" @click="deleteSpuInfo(row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 20]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="prev, pager, next, jumper,->,sizes,total "
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog v-model="dialogSkuInfoVisible">
      <el-table :data="skuList">
        <el-table-column property="skuName" label="名称" width="150" />
        <el-table-column property="price" label="价格(元)" width="200" />
        <el-table-column property="weight" label="重量(KG)" />
        <el-table-column label="默认图片">
          <template v-slot='{row}'>
            <el-image style="width: 100px; height: 100px" :src="row.skuDefaultImg"></el-image>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>
<script lang="ts" setup>
import { Plus, InfoFilled, Delete, Edit } from '@element-plus/icons-vue';
import { ref, watch } from "vue";
import { ElMessage } from 'element-plus';
import {reqFindBySpuId} from "@/api/product/sku";
import { reqGetSpuListData , reqDeleteOneSpuInfo} from "@/api/product/spu";
import { useCategorySelectorInfoStore } from "@/stores/categorySelector";
const skuList = ref([])
const skuName = ref('')
const dialogSkuInfoVisible = ref(false)
const categoryStore = useCategorySelectorInfoStore();
const spuList = ref([]);
const currentPage = ref(1); // 当前页码
const pageSize = ref(5); // 每页显示的条数
const total = ref(0); // 商品总数量
// 分页器
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
const loading = ref(true);
// 获取页面的spu页面数据
const getSpuListData = async () => {
  const result = await reqGetSpuListData(currentPage.value, pageSize.value, categoryStore.category3Id as number);
  console.log(result)
  spuList.value = result.records;
  total.value = result.total;
};
const emits = defineEmits(['changeScene','getData'])
watch(
  () => categoryStore.category3Id,
  category3Id => {
    if (!category3Id) {
      // 清空渲染的列表页
      return;
    }
    //
    getSpuListData();
  },
  { immediate: true }
);
// 4. 每页显示多少条触发的时候，也要重新渲染页面
const handleSizeChange = (val: number) => {
  // console.log(`${val} items per page`);
  pageSize.value = val;
  currentPage.value = 1; // 重置为1
  getSpuListData()
};

// 3. 页码发生变化的时候 重新发送请求
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`);
  currentPage.value = val;
  getSpuListData()
};
//  删除一条SPUinfo
const deleteSpuInfo = async(id:number)=>{
  await reqDeleteOneSpuInfo(id)
  ElMessage.success('删除数据成功...')
  getSpuListData()
}
//  点击编辑并跳转
const toEditSpuInfo=row=>{
  console.log('row',row)
  emits('changeScene',2)
  emits('getData',{
    ...row,
    spuImageList:[],
    spuSaleAttrList:[]
  })
}
// 
const toCheckSkuList = async(row)=>{
  dialogSkuInfoVisible.value = true
  skuName.value=row.spuName
  skuList.value=await reqFindBySpuId(row.id)
}
const toAddskuInfo=row=>{
  emits('changeScene',3)
  emits('getData',row)
}
</script>

<style></style>
